﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Input.Touch;
using System.Windows.Threading;

namespace TestWP
{
    public partial class MainPage : PhoneApplicationPage
    {
        PhotoChooserTask pct = new PhotoChooserTask();
        GameCore.Core core;
        DispatcherTimer dt;
        List<WriteableBitmap> lImg;
        Image[,] imgArr = new Image[4, 4];
        int timePlayed;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            pct.Completed += pct_Completed;
            imgArr[0, 0] = img1;
            imgArr[0, 1] = img2;
            imgArr[0, 2] = img3;
            imgArr[0, 3] = img4;
            imgArr[1, 0] = img5;
            imgArr[1, 1] = img6;
            imgArr[1, 2] = img7;
            imgArr[1, 3] = img8;
            imgArr[2, 0] = img9;
            imgArr[2, 1] = img10;
            imgArr[2, 2] = img11;
            imgArr[2, 3] = img12;
            imgArr[3, 0] = img13;
            imgArr[3, 1] = img14;
            imgArr[3, 2] = img15;
            imgArr[3, 3] = img16;
            dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(0, 0, 1); // 1 Seconds
            dt.Tick += new EventHandler(dt_Tick);
            
        }

        void dt_Tick(object sender, EventArgs e)
        {
            lbTiming.Text = "Time: " + getTimeText(timePlayed++);
        }



        String getTimeText(int secVal)
        {

            String timeString = "";
            int seconds = (int)(secVal % 60);
            int minutes = (int)((secVal / 60) % 60);
            int hours = (int)((secVal / (60 * 60)) % 60);
            if (hours <= 9)
                timeString = "0" + Convert.ToString(hours) + " : ";
            else
                timeString = Convert.ToString(hours) + " : ";
            if (minutes <= 9)
                timeString = timeString + "0" + Convert.ToString(minutes) + " : ";
            else
                timeString = timeString + Convert.ToString(minutes) + " : ";
            if (seconds <= 9)
                timeString = timeString + "0" + Convert.ToString(seconds) + " ";
            else
                timeString = timeString + Convert.ToString(seconds) + " ";
            return timeString;
        }


        private void rndNumber()
        {
            
        }


        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            pct.Show();
        }

        void pct_Completed(object sender, PhotoResult e)
        {
            var bm = new WriteableBitmap(0, 0);
            bm.SetSource(e.ChosenPhoto);
            core = new GameCore.Core();
            core.initArr(4, 15);
            core.rndArr();
            lImg = GameCore.ImageProcess.cropImg(bm, GameCore.Core.WidthLenght);
            for (int i = 0; i < GameCore.Core.WidthLenght; i++)
            {
                for (int j = 0; j < GameCore.Core.WidthLenght; j++)
                {
                    imgArr[i, j].Source = lImg[core.ArrNumber[i, j]];
                }
            }
            timePlayed = 0;
            dt.Start();
        }

        private void StackPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            //Check if touch Gesture is available  
            if (TouchPanel.IsGestureAvailable)
            {
                // Read the gesture so that you can handle the gesture type  
                GestureSample gesture = TouchPanel.ReadGesture();
                switch (gesture.GestureType)
                {
                    case GestureType.HorizontalDrag:
                        MessageBox.Show("Volume Up");
                        break;
                    case GestureType.VerticalDrag:
                        MessageBox.Show("Volume Down");
                        break;
                    
                    default:
                        //do something  
                        break;
                }
            }
        }
    }
}